#!D:/CODE/python
# -*- coding: utf-8 -*-
# @Time : 2020/5/25 10:27
# @Author : Alexdong
# @Site : 
# @File : 自己实现可以迭代的对象.py
# @Software: PyCharm
# Functional description:
from collections.abc import Iterable
from collections.abc import Iterator

class Classmate(object):
    def __init__(self):
        self.names = list()
        self.current_num = 0
    def add(self, name):
        self.names.append(name)

    def __iter__(self):
        """如果想要一个对象称为一个 可迭代的对象，就必须实现__iter__方法"""
        # 返回对象的引用
        return self   # 迭代器

    def __next__(self):
        if self.current_num < len(self.names):
            ret = self.names[self.current_num]
            self.current_num += 1
            return ret
        else:
            raise StopIteration

classsmate = Classmate()
classsmate.add("老王")
classsmate.add("王二")
classsmate.add("张三")

print("判断classmate是否是可以迭代的对象：", isinstance(classsmate, Iterable))
classmate_iterator = iter(classsmate)
print("判断classmate_iterator是否是迭代器：", isinstance(classmate_iterator, Iterator))
# print(next(classmate_iterator))
for name in classsmate:
    print(name)
